<?php
include "Includes/solotrn_sesion.php";
include "Includes/soloFun_Generales.php";
require_once "config.php";
require_once "Includes/conexion.class.php";

//'----
//'Ingresa aqui cuando da submit en la forma para modificar o eliminar definitivo
//'----

if ($_REQUEST["hidId1"] != "") {
    $arrQueries = array();
    $tabla = "solotb_nivel4";
    $txtTiempo = $_REQUEST["txtTiempo"];
    $cmbTiempo = $_REQUEST["cmbTiempo"];
    switch ($_REQUEST["opc"]) {

//         '===============================
//         'MODIFICAR
//         '===============================

        case "M":
            $txtTiempo_hid = $_REQUEST["txtTiempo_hid"];
            $cmbTiempo_hid = $_REQUEST["cmbTiempo_hid"];

            $tipo = "UPDATE";

            $strSql="$tipo $tabla set ";
            $strSql .= "des='"  . $_REQUEST["newds4"] . "' ";
            $strSql .= " where id4=" . $_REQUEST["hidId1"];
//echo $txtTiempo_hid;
            if(!empty($txtTiempo) && is_numeric($txtTiempo) && ($txtTiempo_hid != $txtTiempo)){
                array_push($arrQueries, $strSql);
                array_push($arrQueries, crearQueryLog($tabla, $tipo, $strSql));

                //preguntar si existe para saber si hacer insert o update
                $sql = "SELECT COUNT(*) FROM tramite_tiempo WHERE id_tramite = {$_REQUEST["hidId1"]}";
                $num = SoloFun_ExecSql($sql, $arr);
                if($num < 1){
                    $tipo = "INSERT";
                    $tabla = "tramite_tiempo";
                    $strSql = "$tipo into $tabla values ({$_REQUEST["hidId1"]}, $txtTiempo, $cmbTiempo)";
                    array_push($arrQueries, $strSql);
                    array_push($arrQueries, crearQueryLog($tabla, $tipo, $strSql));
                }else{
                    $tipo = "UPDATE";
                    $tabla = "tramite_tiempo";
                    $strSql = "$tipo $tabla SET duracion = $txtTiempo,
                                unidad_tiempo = $cmbTiempo
                                WHERE id_tramite = {$_REQUEST["hidId1"]}";
                    array_push($arrQueries, $strSql);
                    array_push($arrQueries, crearQueryLog($tabla, $tipo, $strSql));
                }
            }

            //if (($_REQUEST["dsniv4"]==$_REQUEST["newds4"]) && (!empty($txtTiempo) && is_integer($txtTiempo) && ($txtTiempo_hid == $txtTiempo))) {
                //header("location: trn_SubCategorias2.php?SoloError=0&opc=3");
            //}

            //if ($err <> 0) {
                //header("location: trn_SubCategorias2.php?SoloError=1&opc=3");
            //}



//         '===============================
//         'ELIMINAR
//         '===============================
            break;
        case "E":
            $tipo = "DELETE";

            $Combo1=$_REQUEST["hidId1"];

            //preguntar si existe para saber si hacer insert o update
            $sql = "SELECT COUNT(*) FROM tramite_tiempo WHERE id_tramite = {$_REQUEST["hidId1"]}";
            $num = SoloFun_ExecSql($sql, $arr);
            if($num > 0){
                $tipo = "DELETE";
                $tabla = "tramite_tiempo";
                $strSql = "$tipo FROM $tabla WHERE id_tramite = $Combo1";

                array_push($arrQueries, $strSql);
                array_push($arrQueries, crearQueryLog($tabla, $tipo, $strSql));
            }

            $tabla = "solotb_nivel4";
            $strSql="$tipo from $tabla";
            $strSql .= "   where id4= $Combo1";
            array_push($arrQueries, $strSql);
            array_push($arrQueries, crearQueryLog($tabla, $tipo, $strSql));

//         '===============================
//         'INGRESAR
//         '===============================
            break;
        default:
            //txtTiempo
            $tipo = "INSERT";
            $strSql="$tipo into $tabla values (";
            $strSql .= $_REQUEST["hidId1"] . ",'";
            $strSql .=$_REQUEST["ATxtDesc"] . "')"; 		 

            array_push($arrQueries, $strSql);
            array_push($arrQueries, crearQueryLog($tabla, $tipo, $strSql));
//echo $txtTiempo;
            if(!empty($txtTiempo) && is_numeric($txtTiempo)){
                $tipo = "INSERT";
                $tabla = "tramite_tiempo";
                $strSql = "$tipo into $tabla values ({$_REQUEST["hidId1"]}, $txtTiempo, $cmbTiempo)";
                array_push($arrQueries, $strSql);
                array_push($arrQueries, crearQueryLog($tabla, $tipo, $strSql));
            }
            break;
    }

    
    //array_push($arrQueries, $strSql);
    //array_push($arrQueries, crearQueryLog($tabla, $tipo, $strSql));
//printArray($arrQueries);
    $oConexion = new Conexion($WORKFLOW["motor_base"], $WORKFLOW["db_usuario"], $WORKFLOW["db_password"], 
            $WORKFLOW["basedatos"], $WORKFLOW["servidor"], $WORKFLOW["flags"], $WORKFLOW["encoding"]);

    if(!$oConexion->EjecutarTransaccion($arrQueries)) {
        echo $oConexion->getErrorMessage();
        $strMsg="trn_SubCategorias2.php?SoloError=1&opc=3";
    }else {
        $strMsg="trn_SubCategorias2.php?SoloError=0&opc=3";
    }
    $oConexion->Cerrar();
//echo $strMsg;
    header ("Location: trn_SubCategorias2.php?SoloError=0&opc=3");
//'	  response.write strMsg
//'	  response.end

}

//'-------------------------------------------
//'Ingresa aqui cuando la primera vez ingresa
//'-------------------------------------------
switch ($_REQUEST["opc"]) {
    case "M":
        $boton ="<input type='submit' name='CmdGrabar' value='Modificar' class='SoloStyle_Boton'>";
        $oculto="<input type='hidden' name='opc' value='M'>";
        $titulo="Modificaci&oacute;n de Tr&aacute;mite";
        $disabledC = "readonly";
        break;
    case "E":
        $boton ="<input type='submit' name='CmdGrabar' value='Eliminar' class='SoloStyle_Boton'>";
        $oculto="<input type='hidden' name='opc' value='E'>";
        $titulo="Eliminaci&oacute;n de Tr&aacute;mite";
        break;
}
$SoloSQL = "SELECT * FROM tramite_tiempo WHERE id_tramite = ".$_REQUEST["id4"];
soloFun_ExecSQL ($SoloSQL, $SoloArrDuracion);
//echo $SoloSQL;
//printArray($SoloArrDuracion);
$txtTiempo = $SoloArrDuracion[0][1];

$soloSQL = "select id,des from tiempo";
soloFun_ExecSQL ($soloSQL,$SoloArrTiempo);
$StrCboTiempo = SoloFun_CargaCombo($SoloArrTiempo,"cmbTiempo","",$SoloArrDuracion[0][2],"style='width: 80px' class='SoloStyle_Texto'","");
?>
<html>
    <head>
        <title>Documentacion</title>
        <link rel="stylesheet" href="SoloStyles/Solostyles.css" type="text/css">
    </head>

    <body topmargin=0 leftmargin=0>
        <!-- Include File="includes/trn_header.asp" -->
        <p align="center" class="SoloStyle_TituloPaginas"><?php echo $titulo;?></p>

        <form name="frmIngreso" action="trn_GrabaSubCategoria2.php" method="POST">

            <table border="0" width="70%" align="center">
                <tr>
                    <td class="SoloStyle_Etiqueta">Tr&aacute;mite</td>
                    <td class="SoloStyle_Texto">
                        <?php
                        $strSql="select * from solotb_nivel4 where id4=" . $_REQUEST["id4"];

                        $oConexion = new Conexion($WORKFLOW["motor_base"], $WORKFLOW["db_usuario"], $WORKFLOW["db_password"], 
                                $WORKFLOW["basedatos"], $WORKFLOW["servidor"], $WORKFLOW["flags"], $WORKFLOW["encoding"]);
                        $oConexion->EjecutarQuery($strSql);
                        $res=$oConexion->getData();
                        $Des4 = $res[0]["des"];
                        $oConexion->Cerrar();
                        ?>
                        <input type="text" maxlength="50" size="25" name="newds4" class="SoloStyle_Texto" value="<?php echo $_REQUEST["ds4"];?>" <?php if($_REQUEST["opc"] == "E"){?> readonly <?php }?>>
                        <input type="hidden" name="dsniv4" value="<?php echo $_REQUEST["ds4"];?>">
                        <input type="hidden" name="hidId1" value="<?php echo $_REQUEST["id4"];?>">
                    </td>
                </tr>
                <tr>
                    <td class="SoloStyle_Etiqueta">Duraci&oacute;n</td>
                    <td>
                        <input name="txtTiempo" type="text" class="SoloStyle_Texto" id="txtTiempo" size="20" maxlength="2" value="<?php echo $txtTiempo ?>" <?php if($_REQUEST["opc"] == "E"){?> readonly <?php }?>>
                        <input type="hidden" name="txtTiempo_hid" id="txtTiempo_hid" value="<?php echo $txtTiempo;?>"/>
                        <input type="hidden" name="cmbTiempo_hid" id="cmbTiempo_hid" value="<?php echo $cmbTiempo;?>"/>
                        <?php echo $StrCboTiempo;?>
                    </td>

                </tr>
            </table>

            <br><center><?php echo $boton;
echo $oculto;?></center>
        </form>

    </body>
</html>
